#include "mobile.h"
#include <iostream>

using namespace std;

void MobileUnit::move(int distance){

	for(int i=0; i<distance; ++i){
		moveStep();
	}
}

void MobileUnit::turnleft(){
	loc.setDirection(turn(loc.getdirection(), false));
}
void MobileUnit::turnright(){
	loc.setDirection(turn(loc.getdirection(),true));
}

/*int MobileUnit::getXlocation(){
	return loc.getXlocation();
}
int MobileUnit::getYlocation(){
	return loc.getYlocation();
}
int MobileUnit::getdirection(){
	return loc.getdirection();
}*/

Location::Direction MobileUnit::turn(Location::Direction current, bool right)
{
	if(right)
	{
		switch(current)
		{
		case(Location::north):
			return Location::east;
		case(Location::east):
			return Location::south;
		case(Location::south):
			return Location::west;
		case(Location::west):
			return Location::north;
		}
	}
	else
	{
		switch(current)
		{
		case(Location::north):
			return Location::west;
		case(Location::east):
			return Location::north;
		case(Location::south):
			return Location::east;
		case(Location::west):
			return Location::south;
		}
	}

}


void MobileUnit::printStatus(){

	cout << "Location is ";
	loc.printlocation();
	cout << endl;
	cout << "Facing ";
	loc.printdirection();
	cout << endl;
}

void MobileUnit::moveStep(){
	if (loc.getdirection()==Location::north)
	{
		if(world->getMaxY() > loc.getYlocation() + 1){
			loc.moveY(1);
		}
	}
	else if (loc.getdirection()==Location::east)
	{
		if(world->getMaxX() > loc.getXlocation() + 1){
			loc.moveX(1);
		}
	}
	else if (loc.getdirection()==Location::south)
	{
		if(loc.getYlocation() > 0){
			loc.moveY(-1);
		}
	}
	else if (loc.getdirection()==Location::west)
	{
		if(loc.getXlocation() > 0){
			loc.moveX(-1);
		}
	}

	updateWorld();
}
